package com.weme.live.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.weme.live.R

interface OnRatingChangedListener {
    fun onRatingChanged(rating: Int)
}

class RatingStar(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    private var normalId: Int = 0
    private var focusId: Int = 0
    private var normalImg: Bitmap
    private var focusImg: Bitmap
    private var number: Int = 5
    private var starSize: Int = dpToPx(40)
    private var spacing: Int = dpToPx(25)
    private var height: Int = 0
    private var width: Int = 0
    private var w0: Float = 0f
    private var i0: Int = 3
    private var mGrade: Int = 0
    var onRatingChangedListener: OnRatingChangedListener? = null

    fun setListener(listener: OnRatingChangedListener) {
        this.onRatingChangedListener = listener
    }

    init {
        val array = context.obtainStyledAttributes(attrs, R.styleable.RatingStar)
        normalId = array.getResourceId(R.styleable.RatingStar_starNormal, 0)
        focusId = array.getResourceId(R.styleable.RatingStar_starFocus, 0)
        normalImg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, normalId), starSize, starSize, true)
        focusImg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(resources, focusId), starSize, starSize, true)
        number = array.getInteger(R.styleable.RatingStar_starNumber, 5)
        array.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        height = starSize
        width = starSize * number + spacing * (number - 1)
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for (i in 0 until number) {
            val x = i * (starSize + spacing)
            if (i <= i0) {
                canvas.drawBitmap(focusImg, x.toFloat(), 0f, null)
                mGrade = i + 1
            } else {
                canvas.drawBitmap(normalImg, x.toFloat(), 0f, null)
            }
        }
        Log.e("msg", "我被调用了！$i0")
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x = event.x // 相对于控件自身的距离
        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                w0 = width.toFloat() / number
                i0 = (x / w0).toInt()
                // 性能优化，减少onDraw()调用
                if (mGrade == i0 + 1) {
                    return true
                }
                invalidate()
                onRatingChangedListener?.onRatingChanged(i0 + 1)
            }
            MotionEvent.ACTION_UP -> {
                onRatingChangedListener?.onRatingChanged(i0 + 1)
            }
        }
        return true
    }

    private fun dpToPx(dp: Int): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density).toInt()
    }
}
